package models

import (
	"gorm.io/gorm"
	"srv/basic/config"
	"strconv"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(11);unique;not null;" json:"mobile" comment:"手机号"`
	Password string `gorm:"type:varchar(255);not null;" json:"password" comment:"密码"`
	NickName string `gorm:"type:varchar(11);not null;" json:"nick_name" comment:"昵称"`
	UType    int64  `json:"u_type" gorm:"type:int;comment:1普通用户 2司机 3管理员"`
	Status   int64  `json:"status" gorm:"type:int;comment:1忙碌中 2空闲中"`
}

func (u *User) TableName() string {
	return "user"
}

func (u *User) GetUserById() error {
	return config.DB.Where("id = ?", u.ID).Find(&u).Error
}

func (u *User) GetDriverId() (count []int, err error) {
	err = config.DB.Model(&u).Select("id").Where("u_type = 2").Where("status = 2").Find(&count).Error
	return
}

func (u *User) AddUser() error { //注册
	return config.DB.Create(&u).Error
}

func (u *User) GetUserInfo() error { //查询信息
	return config.DB.Where("mobile = ?", u.Mobile).Find(&u).Error
}

func (u *User) w() error {
	return config.DB.Update(strconv.Itoa(int(u.ID)), u.UType).Error
}
